package com.etc.cloud.controller;

import com.etc.cloud.entityes.CommonResult;
import com.etc.cloud.entityes.Payment;
import com.etc.cloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @Author kalista
 * @Description
 * @Date 2020/8/16  16:01
 **/
@RestController
@Slf4j
public class OrderController {


    @Autowired
    private RestTemplate restTemplate;

    private final String PAYMENT_URL = "http://localhost:8001/payment";

    private final String PAYMENT_SERVICE = "CLOUD-PAYMENT-SERVICE";


    /**
     * 服务发现   获取服务器信息
     */
    @Autowired
    private DiscoveryClient discoveryClient;


    @Autowired
    LoadBalancer loadBalancer;



    @Autowired
    LoadBalancerClient loadBalancerClient;



    /**
     * 支付添加
     * @param payment
     * @return
     */
    @PostMapping("/payment/create")
    public CommonResult create(Payment payment) {

        return  restTemplate.postForObject(PAYMENT_URL + "/create", payment, CommonResult.class);

    }

    /**
     * 根据id进行查询
     * @param id
     * @return
     */
//    @GetMapping("/payment/get/{id}")
//    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
//
//        return restTemplate.getForObject(PAYMENT_URL + "/get/"+id,CommonResult.class);
//    }


    //服务发现
//    @GetMapping("/payment/get/{id}")
//    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
//
//        List<String> services = discoveryClient.getServices();
//        for (String service : services) {
//            log.info("******** service :={}",service);
//        }
//
//        List<ServiceInstance> instances = discoveryClient.getInstances(PAYMENT_SERVICE);
//        for (ServiceInstance instance : instances) {
//            URI uri = instance.getUri();
//            String host = instance.getHost();
//            int port = instance.getPort();
//            String serviceId = instance.getServiceId();
//            System.out.println(uri + "\t" +  host +"\t" +  port +"\t" +  serviceId);
//        }
//        return restTemplate.getForObject(PAYMENT_URL + "/get/"+id,CommonResult.class);
//    }

//
//    @GetMapping("/payment/get/{id}")
//    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
//
//        ServiceInstance serviceInstance = loadBalancerClient.choose(PAYMENT_SERVICE);
//
//        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()) + "/payment/get/"+id;
//
//        return  restTemplate.getForObject(url,CommonResult.class);
//    }



    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {

        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(PAYMENT_SERVICE);

        ServiceInstance serviceInstance = loadBalancer.instances(serviceInstanceList);

        URI uri = serviceInstance.getUri();

        return restTemplate.getForObject(uri + "/payment/get/" + id,CommonResult.class);

    }


    @GetMapping("/consumer/payment/lb")
    public String getServerPort(){
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(PAYMENT_SERVICE);

        ServiceInstance serviceInstance = loadBalancer.instances(serviceInstanceList);

        URI uri = serviceInstance.getUri();

        return restTemplate.getForObject(uri + "/payment/lb",String.class);
    }

    @GetMapping("/consumer/payment/lb2")
    public String getServerPort2(){
        ServiceInstance choose = loadBalancerClient.choose(PAYMENT_SERVICE);
        return restTemplate.getForObject(choose.getUri() + "/payment/lb",String.class);
    }

}
